/*
@author: stanfor
@date: 2022/9/14 10:37
@Version: 1.0
@last modify time : 2022/9/14 10:37
*/

/** getURLParams (获取 URL Search 参数)方法说明
 * @author: JasonStandFor
 * @description Desc:: 获取 URL 问号后面的 key/value 值，也就是 search 参数
 **/
export const getURLParams = () => {
  const searchPar = new URLSearchParams(window.location.search);
  const paramsObj = {};
  for (const [key, value] of searchPar.entries()) {
    paramsObj[key] = value;
  }
  return paramsObj;
};

/** scrollToTop (滚动条滑动到顶部)方法说明
 * @author: JasonStandFor
 * @description Desc:: 平滑的滑动到页面顶部
 **/
export const scrollToTop = () => {
  window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
};

/** scrollToBottom (滚动条滑动到底部)方法说明
 * @author: JasonStandFor
 * @description Desc:: 平滑的滑动到页面底部
 **/
export const scrollToBottom = () => {
  window.scrollTo({
    top: document.documentElement.offsetHeight,
    left: 0,
    behavior: 'smooth',
  });
};

/** smoothScroll (滚动元素到可见区域)方法说明
 * @author: JasonStandFor
 * @param {HTMLElement|Document} element
 * @description Desc:: 滚动元素到可见区域
 * @example
 例1:
 document.getElementById(element).scrollIntoView({ behavior: 'smooth', });
 例2:
 document.querySelector(element).scrollIntoView({ behavior: 'smooth', });
 **/
export const smoothScroll = (element) => {
  try {
    // TODO: 判断是否为一个 HTML 元素挺麻烦，以后有空再做
    element.scrollIntoView({
      behavior: 'smooth',
    });
  } catch (e) {
    console.error(e);
  }
};

/** goToFullScreen (全屏显示元素)方法说明
 * @author: JasonStandFor
 * @param {HTMLElement|Document} element
 * @description Desc:: 全屏显示元素
 **/
export const goToFullScreen = (element) => {
  try {
    element = element || window.document.body;
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    } else if (element.webkitRequestFullscreen) {
      element.webkitRequestFullScreen();
    }
  } catch (e) {
    console.error(e);
  }
};

/** goExitFullscreen (退出浏览器全屏状态)方法说明
 * @author: JasonStandFor
 * @description Desc:: 退出浏览器全屏状态
 **/
export const goExitFullscreen = () => {
  if (window.document.exitFullscreen) {
    window.document.exitFullscreen();
  } else if (window.document.msExitFullscreen) {
    window.document.msExitFullscreen();
  } else if (window.document.mozCancelFullScreen) {
    window.document.mozCancelFullScreen();
  } else if (window.document.webkitExitFullscreen) {
    window.document.webkitExitFullscreen();
  }
};

/** stopPropagation (停止冒泡事件)方法说明
 * @author: JasonStandFor
 * @param {Event} event
 * @description Desc:: 一种适用于所有平台的防止事件冒泡的方法。
 **/
export const stopPropagation = (event) => {
  try {
    event = event || window.event;
    if (event.stopPropagation) {
      event.stopPropagation();
    } else {
      event.cancelBubble = true;
    }
  } catch (e) {
    console.error(e);
  }
};

/** isMobile (是否在移动端)方法说明
 * @author: JasonStandFor
 * @description Desc:: 设备类型是通过识别浏览器的 userAgent 来确定的。
 **/
export const isMobile = () => {
  // ohos 是鸿蒙系统标识
  return !!navigator.userAgent.match(
    /(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone|ohos)/i
  );
};

/** isAndroid (是否为安卓机型)方法说明
 * @author: JasonStandFor
 **/
export const isAndroid = () => {
  return /android/i.test(navigator.userAgent.toLowerCase());
};

/** isIOS (是否为 iPhone 机型)方法说明
 * @author: JasonStandFor
 **/
export const isIOS = () => {
  const reg = /iPhone|iPad|iPod|iOS|Macintosh/i;
  return reg.test(navigator.userAgent.toLowerCase());
};

/** isHarmonyOS (是否为鸿蒙机型)方法说明
 * @author: JasonStandFor
 **/
export const isHarmonyOS = () => {
  return /ohos/i.test(navigator.userAgent.toLowerCase());
};

/** textOverflowHide (文本溢出隐藏)方法说明
 * @author: JasonStandFor
 * @param {number|string} lines - 溢出前显示的文本行数
 **/
export const textOverflowHide = (lines) => {
  return {
    textOverflow: 'ellipsis',
    whiteSpace: 'normal',
    overflow: 'hidden',
    display: '-webkit-box',
    webkitBoxOrient: 'vertical',
    webkitLineClamp: lines + ''
  }
}

/** getExplorerInfo (获取浏览器类型及其版本)方法说明
 * @author: JasonStandFor
 * @description Desc:: 您可能会遇到各种兼容性问题，这时候可能需要获取浏览器的类型和版本。
 **/
export const getExplorerInfo = () => {
  const t = navigator.userAgent.toLowerCase();
  return t.indexOf('msie') >= 0
    ? {
      // ie < 11
      type: 'IE',
      version: Number(t.match(/msie ([\d]+)/)[1])
    }
    : !!t.match(/trident\/.+?rv:(([\d.]+))/)
      ? {
        // ie 11
        type: 'IE',
        version: 11
      }
      : t.indexOf('edge') >= 0
        ? {
          type: 'Edge',
          version: Number(t.match(/edge\/([\d]+)/)[1])
        }
        : t.indexOf('firefox') >= 0
          ? {
            type: 'Firefox',
            version: Number(t.match(/firefox\/([\d]+)/)[1])
          }
          : t.indexOf('chrome') >= 0
            ? {
              type: 'Chrome',
              version: Number(t.match(/chrome\/([\d]+)/)[1])
            }
            : t.indexOf('opera') >= 0
              ? {
                type: 'Opera',
                version: Number(t.match(/opera.([\d]+)/)[1])
              }
              : t.indexOf('Safari') >= 0
                ? {
                  type: 'Safari',
                  version: Number(t.match(/version\/([\d]+)/)[1])
                }
                : {
                  type: t,
                  version: -1
                };
};

